﻿//===============================================================================
// Copyright © Doan Manh Ha.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
// FOR MORE INFORMATION SEE http://hacodingjournal.blogspot.com
//===============================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;


namespace BA2009.Utility
{
    /// <summary>
    /// In-memory class for managing message
    /// </summary>
    public class MessageManager
    {
        private IList<Message> messages;

        IQueryable<Message> msgQueries;

        /// <summary>
        /// Load message to in-memory dictionary
        /// </summary>
        public void Load()
        {
            if (this.messages == null)
                this.messages = new List<Message>();
            //throw new System.NotImplementedException();
            XElement messages = XElement.Load("messages.xml");
            foreach (XElement x in messages.Elements("message"))
            {
                Message message = new Message();

                message.Code = x.Element("code").Value;
                string type = x.Element("type").Value;
                if (type.Equals("I"))//Information
                {
                    message.Type = MSG_TYPE.INFORMATION;
                }
                else if (type.Equals("Q"))
                {
                    message.Type = MSG_TYPE.QUESTION;
                }
                else if (type.Equals("E"))
                {
                    message.Type = MSG_TYPE.ERROR;
                }
                else
                {
                    message.Type = MSG_TYPE.UNKNOWN;
                }

                //Confirm buttons mode
                string confirm = x.Element("confirm").Value;
                if (confirm.Equals("OK"))
                {
                    message.Confirm = CONFIRM.OK;
                }
                else if (confirm.Equals("YESNO"))
                    message.Confirm = CONFIRM.YESNO;
                else if (confirm.Equals("YESNOCANCEL"))
                    message.Confirm = CONFIRM.YESNOCANCEL;
                else
                    message.Confirm = CONFIRM.UNKNOWN;

                
                message.Content = x.Element("content").Value;

                this.messages.Add(message);
                
                
            }

            this.msgQueries = this.messages.AsQueryable();
            
        }

        /// <summary>
        /// Get message from code
        /// </summary>
        /// <param name="code">code of message</param>
        /// <returns></returns>
        public string GetMessage(string code)
        {
            if (this.messages == null || this.messages.Count == 0)
                throw new InvalidOperationException("Chưa khởi tạo danh sách Message cho hệ thống.");

            var x = (from m in msgQueries
                     where m.Code.Equals(code)
                     select m).Single();

            return x.Content;
        }

        public Message GetEntity(string code)
        {

            if (this.messages == null || this.messages.Count == 0)
                throw new InvalidOperationException("Chưa khởi tạo danh sách Message cho hệ thống.");

            var x = (from m in msgQueries
                     where m.Code.Equals(code)
                     select m).Single();

            return x;
        }

        ///// <summary>
        ///// Show dialog message
        ///// </summary>
        ///// <param name="code">code of the message</param>
        ///// <returns></returns>
        //public Nullable<bool> ShowMessage(Window owner,string code)
        //{
        //    Message msg = this.GetEntity(code);

        //    MessageDialog dialog = new MessageDialog();
        //    dialog.MessageEntity = msg;
        //    dialog.Owner = owner;
        //    dialog.ShowActivated = true;
        //    return dialog.ShowDialog();
        //}

        //public Nullable<bool> ShowMessage(Window owner, string code, params object[] formattedParams)
        //{
        //    Message msg = this.GetEntity(code);

        //    //Becase all message will be cache in ClientContext,
        //    //So if we change content field of Message entity, we must create new one!
        //    Message newMsg = new Message();
        //    newMsg.Code = msg.Code;
        //    newMsg.Confirm = msg.Confirm;
        //    newMsg.Content = msg.Content;
        //    newMsg.Type = msg.Type;
        //    newMsg.Content = string.Format(newMsg.Content, formattedParams);
        //    MessageDialog dialog = new MessageDialog();
            
        //    dialog.MessageEntity = newMsg;
        //    dialog.Owner = owner;
        //    dialog.ShowActivated = true;
        //    return dialog.ShowDialog();
        //}
    }
}
